package com.yhd.rabbit.confirm;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author 二十
 * @since 2022/1/14 6:33 下午
 */
@Component
public class ConfirmMq {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static final String EXCHANGE = "exchange.confirm";
    private static final String QUEUE = "queue.confirm";
    private static final String ROUTING_KEY = "routing.confirm";

    /**
     * 发消息
     */
    public void sender(){
        Message message;

        // 设置messageProperties
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setCorrelationId("yhd");
        messageProperties.setHeader("aaa", "111");
        Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
        message = messageConverter.toMessage("尹会东", messageProperties);

        rabbitTemplate.convertAndSend(EXCHANGE,ROUTING_KEY,message);
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 接收消息
     *
     * @param msg
     * @param message
     * @param channel
     */
    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(value = QUEUE, autoDelete = "false", durable = "true"),
            exchange = @Exchange(value = EXCHANGE, autoDelete = "true", durable = "true"),
            key = {ROUTING_KEY}
    )
    )
    public void receiveMessage(String msg, Message message, Channel channel) {
        try {

            System.out.println("msg = " + msg);
            //int i =1/0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        } catch (Exception e) {
            // 消息是否再次被拒绝！
            System.out.println("发生异常了!");
            // getRedelivered() 判断是否已经处理过一次消息！
            if (message.getMessageProperties().getRedelivered()) {
                System.out.println("消息已重复处理,拒绝再次接收");
                // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } else {
                System.out.println("消息即将再次返回队列处理");
                // 参数二：是否批量， 参数三：为是否重新回到队列，true重新入队
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }


    }
}
